#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
from typing import Annotated
from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db.database import async_get_db
from app.schemes.user import UserCreate, UserModelDetail, UserUpdateBirthday, UserUpdateGender
from app.services.user import user_service, UserService
from app.core.pkg.log import logger
from app.util.jwt_helper import validate_auth_header

user_router = APIRouter(prefix="/user", tags=["用户信息"])


@user_router.post("/create", description='创建用户信息')
async def create_user(user: UserCreate, db: Annotated[AsyncSession, Depends(async_get_db)]):
    logger.debug(f"receive user params: {user.model_dump()}")
    created_user: UserModelDetail = await user_service.create(db=db, object=user)
    return {"success": True, 'data': created_user, "message": "success"}


@user_router.get("/list", description='获取所有用户信息')
async def get_all_user(db: Annotated[AsyncSession, Depends(async_get_db)]):
    all_users: UserModelDetail = await user_service.get_multi(
        db=db,
        schema_to_select=UserModelDetail,
        sort_columns='created_at',
        sort_orders='desc',
    )
    return {"success": True, **all_users, "message": "success"}


@user_router.get("/detail", description='获取当前登录用户详情')
async def get_current_user(
    request: Request,
    db: Annotated[AsyncSession, Depends(async_get_db)]
):
    # 获取当前登录用户的payload_id
    user_payload_id = validate_auth_header(request)
    
    # 创建用户服务实例
    user_service_instance = UserService(db)
    
    # 查询用户信息
    user = await user_service_instance.get_user_by_payload_id(user_payload_id)
    
    if not user:
        return {"success": False, "message": "用户不存在", "data": {}}
    
    # 过滤敏感信息，只返回必要的用户信息
    user_info = {
        "username": user.get("username", ""),
        "gender": user.get("gender", ""),
        "birthday": user.get("birthday", ""),
        "payload_id": user_payload_id,
        "created_at": user.get("created_at", "")
    }
    
    return {
        "success": True, 
        "data": user_info, 
        "message": "获取用户详情成功"
    }


@user_router.put("/update/birthday", description='更新用户生日信息')
async def update_user_birthday(
    request: Request,
    user_data: UserUpdateBirthday,
    db: Annotated[AsyncSession, Depends(async_get_db)]
):
    # 获取当前登录用户的payload_id
    user_payload_id = validate_auth_header(request)
    
    # 创建用户服务实例
    user_service_instance = UserService(db)
    
    # 更新用户生日
    _ = await user_service_instance.update_user_birthday(
        payload_id=user_payload_id,
        birthday=user_data.birthday
    )
    
    return {
        "success": True, 
        "data": {},
        "message": "生日更新成功"
    }


@user_router.put("/update/gender", description='更新用户性别信息')
async def update_user_gender(
    request: Request,
    user_data: UserUpdateGender,
    db: Annotated[AsyncSession, Depends(async_get_db)]
):
    # 获取当前登录用户的payload_id
    user_payload_id = validate_auth_header(request)
    
    # 创建用户服务实例
    user_service_instance = UserService(db)
    
    # 更新用户性别
    _ = await user_service_instance.update_user_gender(
        payload_id=user_payload_id,
        gender=user_data.gender
    )
    
    return {
        "success": True, 
        "data": {},
        "message": "性别更新成功"
    }
